#!/bin/bash

streamCount=$1
echo "测试目标路数：${streamCount}"

Current_PATH=$(cd $(dirname $0) && pwd)
cd ${Current_PATH}
pwd
./stop.sh
sleep 3
./start.sh
sleep 3

ZLMediaKit_URL="127.0.0.1"
ZLMediaKit_secret="035c73f7-bb6b-4889-a715-d9eb2d1925cc"
defaultVhost="__defaultVhost__"

inputScheam="rtmp"
inputHost="192.168.1.162"
inputPort="1935"

inputUrl="${inputScheam}://${inputHost}:${inputPort}/live/test"


for((i=0;i<$streamCount;i++))
do
    outputUrl="${outputScheam}://${outputHost}:${outputPort}/live/ch${i}";
    getUrl="http://${ZLMediaKit_URL}/index/api/addStreamProxy?secret=${ZLMediaKit_secret}&vhost=${defaultVhost}&app=live&stream=ch${i}&url=${inputUrl}&enable_rtsp=0&enable_rtmp=1&enable_hls=0&enable_mp4=0&rtp_type=0"
    echo "${getUrl}"
    wget -qO- "${getUrl}"
    sleep 0.2
    echo "-----------------------------------------------------------"
done

inputScheam="rtmp"
inputHost="127.0.0.1"
inputPort="1935"

outputScheam="rtmp"
outputHost="192.168.1.162"
outputPort="1935"

ffmepgBin="/root/zlm/ffmpeg-4.3.1-amd64-static/ffmpeg"

echo "-----------------------------------------------------------"
for((i=0;i<$streamCount;i++))
do
    inputUrl="${inputScheam}://${inputHost}:${inputPort}/live/ch${i}"
    outputUrl="${outputScheam}://${outputHost}:${outputPort}/live/ch${i}";
    echo "ffmpeg -i ${inputUrl} -c:v copy -c:a copy -f flv ${outputUrl} 1>/dev/null 2>/dev/null &"
    ${ffmepgBin} -i ${inputUrl} -c:v copy -c:a copy -f flv ${outputUrl} 1>/dev/null 2>/dev/null &
    # top -b -n 1 | head -n 5
    sleep 0.2
    echo "-----------------------------------------------------------"
done


